﻿using System;
using System.Linq;
using System.Collections.Generic;

namespace List
{

    class Program
    {
        static void Main(string[] args)
        {

            //整型List
            GenericList<int> intlist = new GenericList<int>();
            for (int x = 0; x < 10; x++)
            {
                intlist.Add(x);
            }
            intlist.getData(intlist);
            intlist.sum(intlist);
            intlist.minMax(intlist);


            //字符串型List
            GenericList<string> strlist = new GenericList<string>();
            for (int x = 0; x < 10; x++)
            {
                strlist.Add("str" + x);
            }
            strlist.getData(strlist);

        }
    }
    //链表节点
    public class Node<T>
    {
        public Node<T> Next { get; set; }
        public T Data { get; set; }

        public Node(T t)
        {
            Next = null;
            Data = t;
        }
    }
    //泛型链表类
    public class GenericList<T>
    {
        private Node<T> head;
        private Node<T> tail;

        //委托
        delegate void MyLambda(T i);
        delegate void MyLambda2(int i);
        public GenericList()
        {
            tail = head = null;
        }

        public Node<T> Head
        {
            get => head;
        }

        public void Add(T t)
        {
            Node<T> n = new Node<T>(t);
            if (tail == null)
            {
                head = tail = n;
            }
            else
            {
                tail.Next = n;
                tail = n;
            }
        }

        //打印
        public void getData(GenericList<T> list)
        {
            MyLambda myLambda = m => Console.WriteLine(m);

            //遍历，打印输出链表元素
            for (Node<T> node = list.Head;
                node != null; node = node.Next)
            {
                myLambda(node.Data);
            }
        }
        //求和
        public void sum(GenericList<int> list)
        {
            int sum = 0;
            MyLambda2 myLambda2 = m => Console.WriteLine(m);
            for (Node<int> node = list.Head;
                node != null; node = node.Next)
            {
                sum+=node.Data;
            }
            Console.Write("总和为：");
            myLambda2(sum);
        }
        //最小值、最大值
        public void minMax(GenericList<int> list)
        {
            MyLambda2 myLambda2 = m => Console.WriteLine(m);
            int min, max = 0;
            for (Node<int> node1 = list.Head;
                node1 != null; node1 = node1.Next)
            {
                for (Node<int> node2 = list.Head;
                    node2 != null; node2 = node2.Next)
                {
                    if (node2.Next != null)
                    {
                        if (node2.Data > node2.Next.Data)
                        {
                            int temp = node2.Data;
                            node2.Data = node2.Next.Data;
                            node2.Next.Data = temp;
                        }
                    }
                }
            }
            Node<int> node3 = list.Head;
            Node<int> node4 = list.tail;
            min = node3.Data;
            max = node4.Data;
            Console.Write("最小值：");
            myLambda2(min);
            Console.Write("最大值：");
            myLambda2(max);
        }
    }
}
